import time, sqlite3, random, sys, os
from datetime import datetime


CREATE_SCRIPT = \
"""CREATE TABLE IF NOT EXISTS password (
  `name`        varchar(100)  NOT NULL,
  `account`     varchar(100)  NOT NULL,
  `password`    char(100)  NOT NULL,
  `creater`     char(32)  NOT NULL,
  `create_time` datetime NOT NULL,
  `update_time`    datetime NOT NULL,
  PRIMARY KEY (`name`)
);"""

HELP =  \
"""create 
add name [size] ([6,20]) default:12 
drop
del name
update name new_password
list
get name [d]
path [db|pro]
e
set_model"""

LEVEL = "info"
PROJECT_PATH = os.path.dirname(os.path.abspath(__file__))
DB_PATH = PROJECT_PATH + "/data/password.db"

ENUM_CHAR = 'ABCDEFGHIGKLMNOPQRSTUVWXYZabcdefghigklmnopqrstuvwxyz0123456789+-*/,.'



# 若不使用init则在命令行直接以字符串打印
from colorama import Fore, init
init(autoreset=True)

def console_print(text):
    info = Fore.CYAN + str(text)
    if text == "" :
        print(end = '')
        return
    print(info)


def getConnecion():
    conn = sqlite3.connect(DB_PATH)
    return conn

def stampToFormat(ctime : float = None):
    timestamp = datetime.strptime(time.ctime(ctime), "%a %b %d %H:%M:%S %Y")
    return str(timestamp)


def generate_password(length = 12):
    password = "".join(random.choices(ENUM_CHAR, k = length))
    return password

def get_level():
    global LEVEL
    return LEVEL

def set_level(new_level):
    global LEVEL
    LEVEL = new_level
    
import pywintypes
# win32clipboard专门用来复制粘贴的
import win32clipboard as wcb
import win32con as wc
def set_clipboard(message : str): 
    # 打开复制粘贴板
    wcb.OpenClipboard()
    # 我们之前可能已经Ctrl+C了，这里是清空目前Ctrl+C复制的内容。但是经过测试，这一步即使没有也无所谓
    wcb.EmptyClipboard()
    # 第二个参数要传入字节
    wcb.SetClipboardData(wc.CF_TEXT, message.encode(sys.getdefaultencoding()))
    # 关闭复制粘贴板
    wcb.CloseClipboard()



import contextlib
@contextlib.contextmanager
def open_cursor(conn):
    cur = None
    try:
        cur = conn.cursor()
        yield cur
    finally:
        cur.close()
        conn.commit()
      
        
def parse_input(input_text : str):
    input_list = list(filter(lambda x : x, input_text.split(' ')))
    order = input_list[0:1]
    return order, input_list[1:]
